package com.nulldev.util.analytics.v2;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.ReflectionUtils;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.extSwing.eAdvancedMessage;
import com.nulldev.util.java.swing.extSwing.lookandfeel.themes.ThemeApplier;
import com.nulldev.util.java.swing.extSwing.resources.eMessageType;
import com.nulldev.util.net.IPUtil;
import com.nulldev.util.os.BIOSUtil;
import com.nulldev.util.os.CPUUtil;
import com.nulldev.util.os.GPUUtil;
import com.nulldev.util.os.OSUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class nAnalytics2Configurator extends JFrame {

	private static nAnalytics2Configurator instance;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 * @return 
	 */
	public static nAnalytics2Configurator main() {
		nAnalytics2Configurator frame = new nAnalytics2Configurator();
		ThemeApplier.applyTheme(frame);
		SwingUtil.centerFrame(frame);
		return frame;
	}

	/**
	 * Create the frame.
	 */
	public nAnalytics2Configurator() {
		this.instance = this;
		
		setTitle("nAnalytics.v2.Configurator");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 548, 686);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		final JCheckBox chckbxAllow = new JCheckBox("Allow CPU Data Collection", nAnalytics2Manager.canReadCPU());
		chckbxAllow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					nAnalytics2Manager.writeCPU(chckbxAllow.isSelected(), (long) ReflectionUtils.fetchField(nAnalytics2Manager.class.newInstance(), "authToken"));
				}catch (Exception ea) {
					ea.printStackTrace();
					final eAdvancedMessage evm = eAdvancedMessage.main(eMessageType.ERROR);
					evm.setWindowTitle("nAnalytics2: Error!");
					evm.setMessageTitle("Error occured.");
					evm.setMessage("Something went wrong during applying settings.");
					evm.waitForOK();
				}
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		
		final JCheckBox chckbxAllowGpuData = new JCheckBox("Allow GPU Data Collection", nAnalytics2Manager.canReadGPU());
		chckbxAllowGpuData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					nAnalytics2Manager.writeGPU(chckbxAllowGpuData.isSelected(), (long) ReflectionUtils.fetchField(nAnalytics2Manager.class.newInstance(), "authToken"));
				}catch (Exception ea) {
					ea.printStackTrace();
					final eAdvancedMessage evm = eAdvancedMessage.main(eMessageType.ERROR);
					evm.setWindowTitle("nAnalytics2: Error!");
					evm.setMessageTitle("Error occured.");
					evm.setMessage("Something went wrong during applying settings.");
					evm.waitForOK();
				}
			}
		});
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		final JCheckBox chckbxAllowNetworkData = new JCheckBox("Allow Network Data Collection", nAnalytics2Manager.canAccessNetwork());
		chckbxAllowNetworkData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					nAnalytics2Manager.writeNetwork(chckbxAllowNetworkData.isSelected(), (long) ReflectionUtils.fetchField(nAnalytics2Manager.class.newInstance(), "authToken"));
				}catch (Exception ea) {
					ea.printStackTrace();
					final eAdvancedMessage evm = eAdvancedMessage.main(eMessageType.ERROR);
					evm.setWindowTitle("nAnalytics2: Error!");
					evm.setMessageTitle("Error occured.");
					evm.setMessage("Something went wrong during applying settings.");
					evm.waitForOK();
				}
			}
		});
		
		JScrollPane scrollPane_2 = new JScrollPane();
		
		final JCheckBox chckbxAllowOsData = new JCheckBox("Allow OS Data Collection", nAnalytics2Manager.canReadOS());
		chckbxAllowOsData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					nAnalytics2Manager.writeOS(chckbxAllowOsData.isSelected(), (long) ReflectionUtils.fetchField(nAnalytics2Manager.class.newInstance(), "authToken"));
				}catch (Exception ea) {
					ea.printStackTrace();
					final eAdvancedMessage evm = eAdvancedMessage.main(eMessageType.ERROR);
					evm.setWindowTitle("nAnalytics2: Error!");
					evm.setMessageTitle("Error occured.");
					evm.setMessage("Something went wrong during applying settings.");
					evm.waitForOK();
				}
			}
		});
		
		JScrollPane scrollPane_3 = new JScrollPane();
		
		JButton btnFinishAndReturn = new JButton("Finish and Return to Prompt");
		btnFinishAndReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		
		JCheckBox chckbxAllowBiosData = new JCheckBox("Allow BIOS Data Collection", nAnalytics2Manager.canReadBIOS());
		chckbxAllowBiosData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					nAnalytics2Manager.writeBIOS(chckbxAllowBiosData.isSelected(), (long) ReflectionUtils.fetchField(nAnalytics2Manager.class.newInstance(), "authToken"));
				}catch (Exception ea) {
					ea.printStackTrace();
					final eAdvancedMessage evm = eAdvancedMessage.main(eMessageType.ERROR);
					evm.setWindowTitle("nAnalytics2: Error!");
					evm.setMessageTitle("Error occured.");
					evm.setMessage("Something went wrong during applying settings.");
					evm.waitForOK();
				}
			}
		});
		
		JScrollPane scrollPane_4 = new JScrollPane();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(btnFinishAndReturn, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(chckbxAllowOsData, Alignment.LEADING)
						.addComponent(scrollPane_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(chckbxAllowNetworkData, Alignment.LEADING)
						.addComponent(scrollPane_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(chckbxAllowGpuData, Alignment.LEADING)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(chckbxAllow, Alignment.LEADING)
						.addComponent(scrollPane_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
						.addComponent(chckbxAllowBiosData, Alignment.LEADING))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(chckbxAllow)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(chckbxAllowGpuData)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(chckbxAllowNetworkData)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(chckbxAllowOsData)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(chckbxAllowBiosData)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane_4, GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnFinishAndReturn))
		);
		
		JTextPane txtpnCheckedbiosUnchecked = new JTextPane();
		txtpnCheckedbiosUnchecked.setText("Checked: " + BIOSUtil.parse().toString()
				+ "\nUnchecked: N/A");
		txtpnCheckedbiosUnchecked.setEditable(false);
		scrollPane_4.setViewportView(txtpnCheckedbiosUnchecked);
		
		JTextPane txtpnCheckedOs = new JTextPane();
		txtpnCheckedOs.setText("Checked:"
				+ "\n - OS Type: " + (OSUtil.isWindows() ? OSUtil.Windows_Type.getType() : OSUtil.getOperatingSystemType())
				+ "\n - Detailed OS Information: " + OSUtil.getOperatingSystemString()
				+ "\nUnchecked: N/A");
		txtpnCheckedOs.setEditable(false);
		scrollPane_3.setViewportView(txtpnCheckedOs);
		
		JTextPane txtpnCheckedLocal = new JTextPane();
		txtpnCheckedLocal.setText("Checked:"
				+ "\n - Local IP: 127.0.0.1"
				+ "\n - Global IP: " + IPUtil.getLocalHostLANAddress_safe()
				+ "\n - Internet IP: " + IPUtil.getPublicIP()
				+ "\nUnchecked: N/A");
		txtpnCheckedLocal.setEditable(false);
		scrollPane_2.setViewportView(txtpnCheckedLocal);
		
		JTextPane txtpnCheckedgpuUnchecked = new JTextPane();
		txtpnCheckedgpuUnchecked.setText("Checked: " + 
				GPUUtil.parse().toString()
				+ "\nUnchecked: N/A");
		txtpnCheckedgpuUnchecked.setEditable(false);
		scrollPane_1.setViewportView(txtpnCheckedgpuUnchecked);
		
		JTextPane txtpnCpuallowedcpu = new JTextPane();
		txtpnCpuallowedcpu.setText("Checked: " + 
				CPUUtil.parse().toString()
				+ "\nUnchecked: N/A");
		txtpnCpuallowedcpu.setEditable(false);
		scrollPane.setViewportView(txtpnCpuallowedcpu);
		contentPane.setLayout(gl_contentPane);
	}

	public static void toggle() {
		if (instance == null) {
			main();
			return;
		}
		instance.setVisible(!instance.isVisible());
	}
}
